package com.wpml.netty;

import java.nio.ByteBuffer;

/**
 * @Description
 * @Date 2021-07-29
 * @Author wangpei
 **/
public class ByteBufferTest {

    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocateDirect(10);
        System.out.println("========初始信息=======  \t" + buffer);
        buffer.put((byte) 1);
        System.out.println("========第一个元素=======\t" + buffer);
        buffer.put((byte) 2);
        System.out.println("========第二个元素=======\t" + buffer);
        buffer.put((byte) 3);
        System.out.println("========第三个元素=======\t" + buffer);

        System.out.println("=================flip=====================");
        buffer.flip();
        System.out.println("========flip 读取=======\t" + buffer);
        buffer.get();
        System.out.println("========  读取元素=======\t" + buffer);

        byte[] dist = new byte[buffer.remaining()];
        System.out.println(dist.length);
        System.out.println("===================compact===================");
        buffer.compact();
        System.out.println("=====compact====== \t" + buffer);
        buffer.put((byte) 4);
        buffer.put((byte) 5);
        buffer.put((byte) 6);

        buffer.flip();

        dist = new byte[buffer.remaining()];
        System.out.println(dist.length);
        ByteBuffer buf = buffer.get(dist);
        buf.compact();
        System.out.println("=====buf====== \t" + buf);


        for (byte b : dist) {
            System.out.println(b);
        }

        System.out.println("============");
        dist = new byte[buf.remaining()];
        System.out.println(dist.length);
    }
}
